//
//  RWChannelModel.m
//  Chat
//
//  Created by 任伟 on 16/1/10.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "RWChannelModel.h"
#import <objc/runtime.h>

@implementation RWChannelModel

+ (instancetype)channelWithDict:(NSDictionary *)dict {

    RWChannelModel *channelModel = [[RWChannelModel alloc] init];

    NSArray *array = [channelModel getProperties];
    
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSString *key = obj;
        
        if (dict[key]) {
            [channelModel setValue:dict[key] forKey:key];
        }
    }];
    
    return channelModel;
}

- (NSArray *)getProperties
{
    unsigned int count;
    
    objc_property_t *properties = class_copyPropertyList(self.class, &count);
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i =0; i< count ; i++) {
        
        objc_property_t pro = properties[i];
        
        const char *name = property_getName(pro);
        
        NSString *property = [[NSString alloc] initWithUTF8String:name];
        
        [array addObject:property];
    }
    
    return array;
}


@end
